/**
 *	Copyright (c) TeamFlamingo And gray 2004-2009
 *	www.me-live.cn / www.i-gray.cn
 *
 *	[ WARNING!!! ] This is a freeware!
 *	[ WARNING!!! ] But, You MUST get author's authorization before using it!
 *
 *	Name : meDeveloperKit.js [ MDK Core ] / Development Code Evolve
 *	Created / Modify : 2009-9-24 / 2009-12-22
 */

var	$	=	function ( id )
{
	return	document.getElementById( id );
}

var	$$	=	function ( tags, obj )
{
	var	obj	=	obj ? obj : document;
	return	obj.getElementsByTagName( tags );
}

var	addEventHandle	=	function ( obj, e, func )
{
	if( obj.addEventListener )	//	W3C
		obj.addEventListener( e, func, false );
	else if( obj.attachEvent )	//	IE
		obj.attachEvent( 'on' + e, func );
}

var	removeEventHandle	=	function ( obj, e, func )
{
	if( obj.removeEventListener )
		obj.removeEventListener( e, func, false );
	else if( obj.detachEvent )
		obj.detachEvent( 'on' + e, func );
}

var	addLoadEvent	=	function ( func, obj )
{
	var	obj	=	obj	? obj : window;
	if( typeof obj.onload != 'function' )
		obj.onload	=	func;
	else
		obj.onload	=	function () { obj.onload(); func(); };
}

var	is_undefined	=	function ( object )
{
	return	typeof	object == 'undefined';
}

var	is_array	=	function ( object )
{
	return	object != null && typeof object == "object" && 'splice' in object && 'join' in object;
}

var	is_object	=	function ( object )
{
	return	typeof object == "object";
}

var	is_function	=	function ( object )
{
	return	typeof object == "function";
}

var	is_string	=	function ( object )
{
	return	typeof object == "string";
}

var	is_number	=	function ( object )
{
	return	typeof object == "number";
}

var	is_element	=	function ( object )
{
	return	object && object.nodeType == 1;
}

var	Class		=	function ( childs, parents )
{
	var	childs	=	childs;											//	[CH]	最后一个参数是父类
	if( !childs )	return;

	var	parents	=	parents && is_object( parents ) ? parents : {};	//	[CH]	解析父类

	function __new__ (){};											//	[CH]	构造临时函数
	__new__.prototype	=	parents.prototype;						//	[CH]	传递prototype

	var	__class__	=	new __new__();								//	[CH]	建立类的Prototype

	for( var x in childs )
	{
		if( x != 'init' )											//	[CH]	跳过构造函数
			__class__[x]	=	childs[x];							//	[CH]	复制类方法
	}

	if( childs.toString != Object.prototype.toString )				//	[CH]	方法覆盖
		__class__.toString = childs.toString;

	if( childs.toLocaleString != Object.prototype.toLocaleString )
		__class__.toLocaleString = childs.toLocaleString;

	if( childs.valueOf != Object.prototype.valueOf )
		__class__.valueOf = childs.valueOf;

	if( childs.init )												//	[CH]	创建构造函数
		var	init	=	childs.init;
	else
		var	init	=	function () {};

	init.prototype	=	__class__;
	init.parents	=	parents;

	return	init;
}

//	[CH]	MDK引导程序
var	meDeveloperKit	=	Class
({
	Version	:	'2.0.1b',
	Name	:	'meDeveloperKit',
	Author	:	'gray',
	Ready	:	{},

	init	:	function ()
	{
		var	$M	=	this;
		var	s	=	$$( 'script' );
		for( var i = 0, j = s.length; i < j; i++ )
		{
			if( s[i].src.indexOf( $M.Name ) != -1 )
			{
				$M.MDK_CORE	=	s[i].src;
				$M.MDK_ROOT	=	$M.MDK_CORE.slice( 0, $M.MDK_CORE.lastIndexOf( '/' ) + 1 );
				break;
			}
		}

		if( $( 'MDKloading' ) && $( 'MDKprogress' ) )
		{
			$( 'MDKprogress' ).innerHTML	=	'开始加载<span>MDK [ Version ' + $M.Version + ' ]</span> 库...';
		}

		var	ext	=	$M.MDK_CORE.indexOf( '?' ) != -1 ? 1 : 0;
		if( !ext )		//	[CH]	如果不存在预载库文件直接返回
			return;
		$M.load( $M.MDK_CORE );

		$M = s = i = j = ext = null;
	},

	load	:	function ( path )
	{
		var	$M	=	this;
		var	regExp	=	new RegExp( $M.Name + '.js', "ig" );
		var	files	=	path.split( "?" )[1].replace( /include\=/ig, '' );
		var	libs	=	files.split( ',' );
		for( var i = 0, j = libs.length; i < j; i++ )
		{
			$M.include( libs[i] );
		}

		$M = regExp = files = libs = i = j;
	},

	//	[CH]	动态加载js脚本
	include	:	function ( libs, func, dir )
	{
		//	[CH]	防止库文件2次载入
		if( this.Ready[libs.toString()] && this.Ready[libs.toString()] == true )
			return;
		var	$M		=	this;
		var	jsDir	=	dir ? dir : $M.MDK_ROOT;
		var	jsLib		=	document.createElement( 'script' );

		jsLib.onload	=	jsLib.onreadystatechange	=	function ( e )
		{
			if( !this.readyState || this.readyState == 'loaded' || this.readyState == 'complete' )
			{
				MDK.Ready[libs.toString()]	=	true;
				if( func )
					func();
				$M = null;
			}
		}

		jsLib.src	=	jsDir + libs + '.js';
		jsLib.type	=	'text/javascript';
		$$( 'head' )[0].appendChild( jsLib );
	},

	//	[CH]	动态载入Css文件
	imports	:	function ( url, media )
	{
		var	cssUrl	=	$$( 'link' );
		if( cssUrl.length > 0/* && cssUrl['href'] == url*/ )
		{
			for( var i = 0, j = cssUrl.length; i < j; i++ )
			{

				var	newUrl	=	url.substr( url.lastIndexOf( '/' ) + 1 );
				if( cssUrl[i].href.indexOf( newUrl ) != -1 )
					return;
			}
		}

		var	media	=	is_undefined( media ) || media === null ? 'all' : media;
		var	styles	=	document.createElement( 'link' );
		styles.setAttribute( "rel", "stylesheet" );
        styles.setAttribute( "type", "text/css" );
        styles.setAttribute( "media", media );
        styles.setAttribute( "href", url );
		$$( 'head' )[0].appendChild( styles );

		cssUrl = media = styles = null;
	}
})

meDeveloperKit.prototype.Tpl	=
{
	parse		:	function ( obj, tpl )
	{
		for( var x in obj )
		{
			//	[CH]	模板的格式必须是 {$string}
			var	regExp	=	new RegExp( '\\{Tpl\\[' + x.toString() + '\\]\\}', 'ig' );
			var	tpl		=	tpl.replace( regExp, obj[x] );
		}
		return	tpl;
	}
}

meDeveloperKit.prototype.Ajax	=
{
	xmlHttp	:	false,
	init	:	function ( type/* get or post */, url, handle/* 回调函数 */, data/* post请求时的数据 */, postDataType )
	{
		var	m	=	this;
		//	[CH]	如果对象不存在,那么创建一个
		//	[CH]	否则终止上一个请求
		if( !m.xmlHttp )
			m.getXmlHttpObject();
		else
			m.xmlHttp.abort();

		//	[CH]	如果创建失败,终止程序继续运行
		if( !m.xmlHttp )
		{
			alert( '创建AJAX对象失败...' );
			return;
		}

		//	[CH]	设定post方法的请求类型
		var	postRequestType	=	!postDataType ? 'application/x-www-form-urlencoded' : 'text/xml';

		try
		{
			m.xmlHttp.onreadystatechange	=	handle;
			m.xmlHttp.open( type, url, true );
			if( type.toLowerCase() == 'get' )
			{
				m.xmlHttp.send(null);
			}
			else
			{
				m.xmlHttp.setRequestHeader( "Content-Type", postRequestType );
				m.xmlHttp.send( data );
			}
		}
		catch(e)
		{
			alert( "请求错误:\n" + "Details: " + e );
		}
		//m = this;
	},

	//	[CH]	获取当前节点的文本内容
	getXmlText	:	function ( element )
	{
		var text = "";
		if ( element != null )
		{
			if ( element.childNodes )
			{
				for ( var i = 0; i < element.childNodes.length; i++ )
				{
					var childNode = element.childNodes[i];
					if ( childNode.nodeValue != null )
						text = text + childNode.nodeValue;
				}
			}
		}
		return text;
	},

	getXmlHttpObject	:	function ()
	{
		var	m	=	this;
		try
		{
			m.xmlHttp = new XMLHttpRequest();
		}
		catch( trymicrosoft )
		{
			try
			{
				m.xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
			}
			catch( othermicrosoft )
			{
				try
				{
					m.xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
				}
				catch( failed )
				{
					m.xmlHttp = false;
				}
			}
		}
		m = null;
	}
}

meDeveloperKit.prototype.Browser	=	( function ()
{
	var	agent	=	navigator.userAgent;

	if( agent.indexOf( 'MSIE' ) != -1 )
	{
		return	{ name : 'IE', version : parseFloat( agent.match( /MSIE\s([\d\.]+)/ )[1] ) };
	}

	if( agent.indexOf( 'Firefox/' ) != -1 )
	{
		return	{ name : 'Firefox', version : parseFloat( agent.match( / Firefox\/(\d+\.\d+)/ )[1] ) };
	}

	if( agent.indexOf( 'Safari/' ) != -1 )
	{
		return	{ name : 'Safari', version : parseFloat( agent.match( / Safari\/([\d\.]+\d$)/ )[1] ) };
	}

	return	{ name : navigator.appName, version : parseFloat( navigator.appVersion ) };
} )();

meDeveloperKit.prototype.Cookie	=
{
	set	:	function ( name, value, expires, path, domain, secure )
	{
		var date	= new Date();
		//date.setTime( date.getTime() );
		if ( expires )
			expires	= expires * 1000 * 60 * 60 * 24;
		else
			expires	= 0;

		date.setTime( date.getTime() + expires );
		//alert( date.toGMTString() );
		//var expires	= today.setTime( today.getTime() + expires );
		document.cookie	= name + '=' + escape( value ) +
				( ( expires ) ? ';expires=' + date.toGMTString() : '' ) + //expires.toGMTString()
				( ( path ) ? ';path=' + path : '' ) +
				( ( domain ) ? ';domain=' + domain : '' ) +
				( ( secure ) ? ';secure' : '' );

		alert( document.cookie );
		//if( this.isset( name ) )
		//return	this.isset( name ) ? 1 : 0 ;
		//alert( this.get( name ) );
	},

	get	:	function ( name )
	{
		var start	= document.cookie.indexOf( name + "=" );
		var len		= start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
			return	null;
		if ( start == -1 )
			return	null;

		var end	= document.cookie.indexOf( ';', len );
		if ( end == -1 )
			end	= document.cookie.length;
		return	unescape( document.cookie.substring( len, end ) );
	},

	del	:	function ( name, path, domain )
	{
		if ( this.get( name ) )
			document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '' ) +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
	},

	isset	:	function ( name )
	{
		return ( document.cookie.indexOf( name + '=' ) != -1 );
	}
}

meDeveloperKit.prototype.Array	=
{
	in_array	:	function ( value, array )
	{
		for( var x in array )
		{
			if( array[x] == value )
				return	true;
		}
		return	false;
	},

	array_push	:	function ( array, value )
	{
		array[array.length]	=	value;
		return	array;
	},

	array_rand :	function ( array )
	{
		if( !is_array( array ) )
			return;

		var	array_length	=	array.length;
		var	rand_seed;//		=	Math.random().toString().substr(2,array_length.toString().length);

		function	getRandomSeed ( array_length )
		{
			var	seed;
			var	result	=	true;

			do
				seed	=	parseInt( Math.random().toString().substr( 2, array_length.toString().length ) );
			while( seed >= array_length );
			return	seed;
		}

		var	rand_seed	=	getRandomSeed( array_length );

		for( var i in array )
		{
			if( i == rand_seed )
				return	array[i];
		}
	}
}

meDeveloperKit.prototype.String	=
{
	trim	:	function ( str )
	{
		return str.toString( String ).replace( /^\s+|\s+$/g, '' );
	},

	ltrim	:	function ( str )
	{
		return	str.replace( /^s*/, '' );
	},

	rtrim	:	function ( str )
	{
		return str.replace( /s*$/, '' );
	},

	nl2br	:	function ( str )
	{
		return	str.replace( /([^>])\n/g, '$1<br />' );
	},

	strip_tags	:	function ( str )
	{
		return	str.replace( /<\/?[^>]+>/ig, '' );
	},

	url		:	function ( str )
	{
		return	/^(http:|ftp:)\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"])*$/.test( str )
	},

	htmlentities	:	function ( str )
	{
		return str.replace( /&/g, '&amp;' ).replace( /</g, '&lt;' ).replace( />/g, '&gt;' );
	},

	html_entity_decode	:	function ( str )
	{
		return str.replace( /&amp;/g, '&' ).replace( /&lt;/g, '<' ).replace( /&gt;/g, '>' );
	}
}

var	Timer	=	( function ()
{
	setInterval( function (){
		if( $( 'MDKTimer' ) )
		{
			//$D	=	new Date();
			$( 'MDKTimer' ).innerHTML = '现在时刻：' + new Date();/* + $D.getYear() + '-' + $D.getMonth() + '-' + $D.getDay() + ' ' + $D.getHours() + ':' + $D.getMinutes() + ':' + $D.getSeconds();*/
			//$D	=	null;
		}
	}, 1000 );
} )();
//Timer();
//MDK.init();
var	MDK	=	new meDeveloperKit();